# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Maintainer: Joan Karadimov <joan.karadimov@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=pywin32
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
url='https://sourceforge.net/projects/pywin32'
pkgdesc='Python for Windows Extensions (mingw-w64)'
license=('PSF')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
pkgver=302
pkgrel=3
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=(https://github.com/mhammond/${_realname}/archive/b${pkgver}.zip
        001-compile-and-setup-fixes.patch
        002-com-extensions-fix.patch
        003-win32-extensions-fix.patch
        004-isapi-fix.patch
        005-swig-fix.patch
        006-mapi-headers-fix.patch
        010-workaround-broken-add-dll-dir.patch)
sha256sums=('33547aa989573dc615bf69c9cc589ef8aefe7ec3858009faf1c26d59f73e4a30'
            '9e1e4d28b1fe89ac6e189b46d047d84619f00ba282fa3b85b8ff2ac3ec54963b'
            'cf83477989cfa3cd72199102af8a89fb2ca9ecf6b9c823aae91f326f2997567f'
            '4ed4f5a99d9be5ef6869ba50e40cca8f977f053624ec85fee8bcba41889096cf'
            '82f7745c4eef1e7c3262f6de71655967090c981c542a2e3b0544fda1649be68b'
            'a364eb52c9e0098516bb08ca5abb687fff4ee1c2e6d4a53dcbe91e60a6820879'
            'a04e4c8ee86606188c55349c4be755c72a9f03830d6f24828aba6bfa251e030d'
            '0168b0ea893cd252201dbde6baad6cfa4b382bc76ba65657606b6c76ce78262f')

case ${MSYSTEM} in
  CLANG*)
    # work around lack of windmc in llvm
    makedepends+=("binutils")
      ;;
esac

prepare() {
    cd "${srcdir}/${_realname}-b${pkgver}"
    patch -p1 < "${srcdir}"/001-compile-and-setup-fixes.patch
    patch -p1 < "${srcdir}"/002-com-extensions-fix.patch
    patch -p1 < "${srcdir}"/003-win32-extensions-fix.patch
    patch -p1 < "${srcdir}"/004-isapi-fix.patch
    patch -p1 < "${srcdir}"/005-swig-fix.patch
    patch -p1 < "${srcdir}"/006-mapi-headers-fix.patch

    # FIXME: https://github.com/msys2-contrib/cpython-mingw/issues/48
    patch -p1 < "${srcdir}"/010-workaround-broken-add-dll-dir.patch

    cd "${srcdir}"
    rm -rf "python-build-${CARCH}" | true
    cp -r "${_realname}-b${pkgver}" "python-build-${CARCH}"
}

build() {
    msg "Python build for ${CARCH}"
    cd "${srcdir}/python-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python setup.py build --skip-verstamp
}

package() {
    cd "${srcdir}/python-build-${CARCH}"
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
      --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 "win32/License.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE.txt"
}
